const IO = require('socket.io');

const SOCKET_PORT = process.env.SOCKET_PORT;

const io = IO(SOCKET_PORT, {
  transports: ['websocket', 'polling']
});

io.on("connection", async (socket) => {
  const id = socket.id;

  const query = socket.handshake.query;

  console.log('socket.io client connected, id: ', id, 'with query', JSON.stringify(query));

  if (query.hotelId) {
    socket.join(query.hotelId); // 加入酒店id room

    socket.emit('res', 'connected!');
  } else {
    socket.disconnect(true);
  }

  socket.on("disconnect", async (reason) => {
    console.log(`socket.id: ${socket.id} disconnected. reason: ${reason}`);
  });
});

module.exports = io;
